package org.one.stone.soup.grfxML;

import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;
import org.one.stone.soup.mjdb.data.field.NullField;
import org.one.stone.soup.xml.XmlElement;
  /*
 * Copyright 2000-2001 Wet-Wired.com Ltd.,
 * Portsmouth England
 * All rights reserved.
 */

/**
	*
	* @author Nik Cross
*/
public abstract class DataGeometry extends NullField implements ITag{
	public static String DEFAULT_ID="_Geometry";

		DataPoint offset = new DataPoint(TagModifier.POINT_TYPE_VECTOR);
		DataPoint offsetView = new DataPoint(TagModifier.POINT_TYPE_POINT);
		DataColor color = new DataColor();
		DataState filled = new DataState(true);
		DataPoint bounds = new DataPoint(TagModifier.POINT_TYPE_BOUNDS);
		DataScale transparency = new DataScale(0);

public DataGeometry() {
	try{
		setId(DEFAULT_ID,this);
	}
	catch(LockException le){} //Dummy catch
}
public boolean buildFromElement(XmlElement element){
	if(element == null)
			return false;

		String eleName = element.getName();

		if(eleName.equals(Token.OFFSET))
		{
			offset = new DataPoint(TagModifier.POINT_TYPE_VECTOR);
			offset.buildFromgrfxML(element,TagModifier.POINT_TYPE_VECTOR);
		}
		else if(eleName.equals(Token.BOUNDS))
		{
			bounds = new DataPoint(TagModifier.POINT_TYPE_BOUNDS);
			bounds.buildFromgrfxML(element,TagModifier.POINT_TYPE_BOUNDS);
		}
		else if(eleName.equals(Token.COLOR))
		{
			color = new DataColor();
			color.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.FILLED))
		{
			filled = new DataState(true);
			filled.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.TRANSPARENCY))
		{
			transparency = new DataScale(1);
	 		transparency.buildFromgrfxML(element);
		}
		else
			return false;

		return true;
}
public boolean copyFromElement(XmlElement element){
	if(element == null)
			return false;

		String eleName = element.getName();

		if(eleName.equals(Token.OFFSET))
		{
			offset.copyFromgrfxML(element,TagModifier.POINT_TYPE_VECTOR);
		}
		else if(eleName.equals(Token.BOUNDS))
		{
			bounds.copyFromgrfxML(element,TagModifier.POINT_TYPE_BOUNDS);
		}
		else if(eleName.equals(Token.COLOR))
		{
			color.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.FILLED))
		{
			filled.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.TRANSPARENCY))
		{
	 		transparency.copyFromgrfxML(element);
		}
		else
			return false;

		return true;
}
public DataPoint getBounds() {
	return bounds;
}
public abstract DataGeometry getClone();
public abstract DataGeometry getClone(String path);
public void getClonedElements(String path,DataGeometry newGeometry)
{
	newGeometry.bounds = bounds.getClone(TagModifier.POINT_TYPE_BOUNDS,path);
	newGeometry.color = color.getClone(path);
	newGeometry.filled = filled.getClone(path);
	newGeometry.offset = offset.getClone(TagModifier.POINT_TYPE_POINT,path);
	newGeometry.transparency = transparency.getClone(path);
	}
public void getClonedElements(DataGeometry newGeometry)
{
	newGeometry.bounds = bounds.getClone();
	newGeometry.color = color.getClone();
	newGeometry.filled = filled.getClone();
	newGeometry.offset = offset.getClone();
	newGeometry.transparency = transparency.getClone();
}
public DataColor getColor() {
	return color;
}
public DataState getFilled() {
	return filled;
}
public abstract Geometry getGeometry(GrfxMLEngine engine);
public DataPoint getOffset() {
	return offset;
}

public DataScale getTransparency() {
	return transparency;
}
public void register(DataLinker store) {

	super.register(store);

	try{
		store.add(offset,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(color,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(filled,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(transparency,this);
	}
	catch(LockException le)
	{}//Dummy catch

}
public void replace(Field oldObj, Field newObj) {

	if(offset==oldObj)
		offset=(DataPoint)newObj;
	if(color==oldObj)
		color=(DataColor)newObj;
	if(filled==oldObj)
		filled=(DataState)newObj;
	if(transparency==oldObj)
		transparency=(DataScale)newObj;
}
public void resetChanged() {
	super.resetChanged();
	offset.resetChanged();
	offsetView.resetChanged();
	color.resetChanged();
	filled.resetChanged();
	transparency.resetChanged();
}
public void setBounds(DataPoint newBounds) {
	bounds = newBounds;
}
protected void setColor(DataColor newColor) {
	color = newColor;
}
protected void setFilled(DataState newFilled) {
	filled = newFilled;
}
protected void setOffset(DataPoint newOffset) {
	offset = newOffset;
}
protected void setTransparency(DataScale newTransparency) {
	transparency = newTransparency;
}
public String togrfxML(String name) {
	String xml = offset.togrfxML(Token.OFFSET,TagModifier.POINT_TYPE_VECTOR);
	xml += color.togrfxML(Token.COLOR);
	xml += filled.togrfxML(DataState.TRUE,Token.FILLED);
	xml += transparency.togrfxML(Token.TRANSPARENCY);
	return xml;
}
public String togrfxML(String name,TagModifier modifier) {
	return togrfxML(name);
}
public String toString()
{
	if(GrfxMLEngine.DEBUG)
		return(togrfxML( DEFAULT_ID.substring(1) ));

	String xml = "<"+DEFAULT_ID.substring(1)+"/>";

	return xml;
}
public void translate(DataTranslation t)
{
	try{
		GrfxMLProcessor.translate(offset,TagModifier.POINT_TYPE_VECTOR,t,offsetView);
	}
	catch(LockException le)
	{
		le.printStackTrace();
	}
}
}
